using System;
using System.Collections;
using UnityEngine;

public class NextMatchDisplay : MonoBehaviour
{
	public UILabel title;

	public UILabel timeRemaining;

	public UISprite flag1Sprite;

	public UISprite flag2Sprite;

	public PlayerCareerLayer careerLayer;

	private PlayerCareerStageDefinition.StageDefinition currentStage;

	private PlayerCareerStageDefinition.GameParameters activeGameParameters;

	private void OnEnable()
	{
		Init();
	}

	private void Init()
	{
		PlayerCareerStages stageDefinition = careerLayer.stageDefinition;
		currentStage = stageDefinition.CurrentStage();
		PlayerCareerStageDefinition.StageDefinition.SubStageDefinition activeSubstage = currentStage.GetActiveSubstage();
		if (activeSubstage != null)
		{
			title.text = activeSubstage.name;
			activeGameParameters = activeSubstage.gameParams;
		}
		else
		{
			title.text = "Match Showdown";
			activeGameParameters = currentStage.gameParams;
		}
		GameConstants.SetFlag(flag1Sprite, GGPlayerSettings.instance.Model.flag);
		GameConstants.SetFlag(flag2Sprite, activeGameParameters.opponent.player.flag);
		StartUpdatingTimeDisplay();
	}

	private void StartUpdatingTimeDisplay()
	{
		StopAllCoroutines();
		StartCoroutine(DoUpdateTimeDisplay());
	}

	private IEnumerator DoUpdateTimeDisplay()
	{
		while (true)
		{
			TimeSpan timeTillNextMatch = activeGameParameters.GetTimeTillNextMatch(currentStage);
			if (timeTillNextMatch.Ticks <= 0)
			{
				break;
			}
			timeRemaining.text = "Starts in: " + GGFormat.FormatTimeSpan(timeTillNextMatch);
			yield return new WaitForSeconds(1f);
		}
		timeRemaining.text = "Starts Now!";
	}
}
